package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.charge;
import yxy.cherry.data.bean.chargeshop;
import yxy.game.pm2.active.module.actcontrol.activeControl;
import yxy.game.pm2.active.module.guanyurecruit.ActiveModule_关羽招募;
import yxy.game.pm2.active.module.paylevelpackage.ActiveModule_支付等级礼包;
import yxy.game.pm2.active.module.sdk.weixin.ActiveModule_在线礼包;
import yxy.game.pm2.active.module.sdk.weixin.ActiveModule_武将分享;
import yxy.game.pm2.active.module.sdk.weixin.ActiveModule_邀请好友;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.module.PayRankModule;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.order.OrderInfo;
import yxy.game.pm2.bean.sdk.WeiXinOtherParm;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.bean.timeline.Timeline;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.task.TaskHandle;

import java.util.HashMap;
import java.util.Map;

public class ActiveModule extends Timeline {

    public static void initAct(String playercode, Boolean isNewPlayer) {

        try {
            AreaBean ab = AreaModule.area(playercode);
            activeControl sai = activeControl.get(ab.getAreaId(), 1001);
            if (null != sai) {
                ActiveModule_开服冲榜 am_开服武将冲榜 = ActiveModule_开服冲榜.get(playercode, 1001);
                am_开服武将冲榜.createAll(playercode, 1001);//红点
                am_开服武将冲榜.provideUnclaimedRewards();//未领取奖励

                ActiveModule_开服冲榜 am_开服竞技冲榜 = ActiveModule_开服冲榜.get(playercode, 1002);
                am_开服竞技冲榜.createAll(playercode, 1002);
                am_开服竞技冲榜.provideUnclaimedRewards();

                ActiveModule_开服冲榜 am_开服武将等级冲榜 = ActiveModule_开服冲榜.get(playercode, 1003);
                am_开服武将等级冲榜.createAll(playercode, 1003);
                am_开服武将等级冲榜.provideUnclaimedRewards();

                ActiveModule_开服冲榜 am_开服联盟boos冲榜 = ActiveModule_开服冲榜.get(playercode, 1004);
                am_开服联盟boos冲榜.createAll(playercode, 1004);
                am_开服联盟boos冲榜.provideUnclaimedRewards();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ActiveModule_心仪武将活动.get(playercode, isNewPlayer);
        } catch (Exception e) {
            e.printStackTrace();
        }


        {
            try {
                ActiveModule_每日首充.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        {
            try {
                ActiveModule_超值首冲.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        {
            try {
                ActiveModule_月卡.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_VIP.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_签到.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_每日礼包.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_每周特惠.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_每月特惠.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_每日累冲.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_七日登陆.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_七日目标.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_夺宝.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_冲榜.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_等级礼包.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_累充天数.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_限时抢购.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_超值月基金.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_限时礼包_Person.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_关卡基金.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_限时兑换.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_每日特惠.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_十连预抽.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_限时挑战.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_周卡.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_神魔卡.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_翻牌.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_征战日志.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        {
            try {
                ActiveModule_节日活动.get(playercode, isNewPlayer);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        try {
            ActiveModule_支付等级礼包.get(playercode, isNewPlayer);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ActiveModule_关羽招募.get(playercode, isNewPlayer);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ActiveModule_武将直升活动.get(playercode, isNewPlayer);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ActiveModule_心愿瓶.get(playercode, isNewPlayer);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ActiveModule_国庆寻宝.get(playercode, isNewPlayer);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (WeiXinOtherParm.get(playercode) != null) {
            try {
                ActiveModule_在线礼包.get(playercode);
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                ActiveModule_武将分享.get(playercode);
            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                ActiveModule_邀请好友.get(playercode);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    static public int canbuy() {
        return 0;
    }

    static public void HandleActiveAddVipexp(String pcode, OrderInfo oi) {
        String myorderid = oi.getMyorderid();

        // 锁单到完成单 TODO:
        switch (oi.getActtype()) {
            case ActiveModule_每日礼包.acttype: {
                try {
                    ActiveModule_每日礼包 am = ActiveModule_每日礼包.get(pcode, false);
                    am.locktofinish(oi.getGoodsid(), myorderid);
//				break;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case ActiveModule_每周特惠.acttype: {
                try {
                    ActiveModule_每周特惠 am = ActiveModule_每周特惠.get(pcode, false);
                    am.locktofinish(oi.getGoodsid(), myorderid);
//				break;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case ActiveModule_每月特惠.acttype: {
                try {
                    ActiveModule_每月特惠 am = ActiveModule_每月特惠.get(pcode, false);
                    am.locktofinish(oi.getGoodsid(), myorderid);
//				break;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case ActiveModule_特权商城.acttype: {
                try {
                    // 特权商城 处理
                    ActiveModule_特权商城 am = ActiveModule_特权商城.get(pcode);
                    int goodsid = oi.getGoodsid();
                    am.unlock(goodsid, myorderid);
                    Map<String, chargeshop> css = DataCenter.getAllData(chargeshop.class);
                    for (String key : css.keySet()) {
                        chargeshop cs = css.get(key);
                        if (cs.getPurchasetype() == 1) {
                            if (cs.getPurchasever() == goodsid) {
                                am.addprerogative(cs.getId(), cs);
                            }
                        }
                    }
//				break;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case ActiveModule_成长基金.acttype: {
                try {
                    ActiveModule_成长基金 am = ActiveModule_成长基金.get(pcode);
                    am.handlebuy();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case ActiveModule_养成礼包.acttype: {
                try {
                    ActiveModule_养成礼包 am = ActiveModule_养成礼包.get(pcode);
                    am.unlockGiftPackageAndSetBuyTime(oi.getGoodsid(), oi.getUpDateTime());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case ActiveModule_招募令礼包.acttype: {
                try {
                    ActiveModule_招募令礼包 am = ActiveModule_招募令礼包.get(pcode);
                    am.unlockGiftPackageAndSetBuyTime(oi.getGoodsid(), oi.getUpDateTime());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case ActiveModule_武将直升活动.acttype: {
                try {
                    ActiveModule_武将直升活动 am = ActiveModule_武将直升活动.get(pcode, false);
                    am.unlockGiftPackageAndSetBuyTime(oi.getGoodsid(), oi.getUpDateTime());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case ActiveModule_心仪武将活动.acttype: {
                try {
                    ActiveModule_心仪武将活动 am = ActiveModule_心仪武将活动.get(pcode, false);
                    am.unlockGiftPackageAndSetBuyTime(pcode, oi.getGoodsid(), oi.getUpDateTime());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }

            case ActiveModule_心愿瓶.acttype: {
                try {
                    ActiveModule_心愿瓶 am = ActiveModule_心愿瓶.get(pcode, false);
                    am.unlockGiftPackageAndSetBuyTime(oi.getGoodsid());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }

            case ActiveModule_国庆寻宝.acttype: {
                try {
                    ActiveModule_国庆寻宝 am = ActiveModule_国庆寻宝.get(pcode, false);
                    am.unlockGiftPackageAndSetBuyTime(oi.getGoodsid());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }

            case ActiveModule_限时礼包_Person.acttype: {
                try {
                    ActiveModule_限时礼包_Person am = ActiveModule_限时礼包_Person.get(pcode, false);
                    am.handlebuy(oi.getGoodsid(), oi.getMyorderid());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case ActiveModule_关卡基金.acttype: {
                try {
                    ActiveModule_关卡基金 am = ActiveModule_关卡基金.get(pcode, false);
                    am.dealbuy(oi.getGoodsid());
//				am.unlockorder(oi.getGoodsid());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }

            case ActiveModule_每日特惠.acttype: {
                try {
                    ActiveModule_每日特惠 am = ActiveModule_每日特惠.get(pcode, false);
                    am.dealbuy(oi.getGoodsid());
//				am.unlockorder(oi.getGoodsid());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }

//		case ActiveModule_周卡.acttype: {
//			try {
//				ActiveModule_周卡 am = ActiveModule_周卡.get(pcode);
//				am.unlockorder(oi.getGoodsid(), oi.getMyorderid());
//				am.handlebuy(oi.getGoodsid(), oi.getMyorderid(), oi.getAddvipexp());
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//			break;
//		}

            case ActiveModule_支付等级礼包.acttype: {
                try {
                    ActiveModule_支付等级礼包 am_支付等级礼包 = ActiveModule_支付等级礼包.get(pcode, false);
                    am_支付等级礼包.unlockOrderAndSetBuyTime(oi.getGoodsid(), DateTime.now());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }

            case ActiveModule_关羽招募.acttype: {
                try {
                    ActiveModule_关羽招募 am_关羽招募 = ActiveModule_关羽招募.get(pcode, false);
                    am_关羽招募.buyunlockorder(oi.getGoodsid());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }

            case ActiveModule_神魔卡.acttype: {
                try {
                    ActiveModule_神魔卡 am_神魔卡 = ActiveModule_神魔卡.get(pcode, false);
                    am_神魔卡.handlebuy(oi.getGoodsid(), oi.getMyorderid());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }

            case ActiveModule_翻牌.acttype: {
                try {
                    ActiveModule_翻牌 am_翻牌 = ActiveModule_翻牌.get(pcode, false);
                    am_翻牌.handlebuy(oi.getGoodsid(), oi.getMyorderid());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }

            case ActiveModule_超值月基金.acttype: {
                try {
                    ActiveModule_超值月基金 am_超值月基金 = ActiveModule_超值月基金.get(pcode, false);
                    am_超值月基金.handlebuy(oi.getGoodsid());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }

            case ActiveModule_限时抢购.acttype: {
                try {
                    ActiveModule_限时抢购 am_限时抢购 = ActiveModule_限时抢购.get(pcode, false);
                    am_限时抢购.orderFinish(oi.getGoodsid(), oi.getMyorderid());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
            case ActiveModule_节日活动.acttype: {
                try {
                    ActiveModule_节日活动 am = ActiveModule_节日活动.get(pcode, false);
                    am.handlebuy(oi.getGoodsid(), oi.getMyorderid());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }

        }

        try {
            // 添加vip经验
            new CurrencyHandle().addCurrency(pcode, CCReason.用户购买,
                    Currency.create(CurrencyType.CT_VIP经验, 0, oi.getAddvipexp()));
        } catch (Exception e) {
            e.printStackTrace();
        }


        try {
            // 添加充值金额
            new PayRankModule().addAndSet(pcode, oi.getGoodsprice());
            TaskHandle.handle(pcode, TaskType.统计型_充值_累计_达到, oi.getGoodsprice());
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ActiveModule_每日首充 am_每日首充 = ActiveModule_每日首充.get(pcode, false);
            if (am_每日首充.running()) {
                am_每日首充.addtopupexp(oi.getAddvipexp());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ActiveModule_超值首冲 am_超值首冲 = ActiveModule_超值首冲.get(pcode, false);
            if (am_超值首冲.running()) {
                am_超值首冲.addtopupexp(oi.getAddvipexp(), oi.getGoodsprice());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ActiveModule_签到 am_签到 = ActiveModule_签到.get(pcode, false);
            am_签到.dealpay();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ActiveModule_月卡 am_月卡 = ActiveModule_月卡.get(pcode, false);
            am_月卡.addvipexp(oi.getAddvipexp());
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ActiveModule_元宝首冲 am_元宝首冲 = ActiveModule_元宝首冲.get(pcode);
            am_元宝首冲.handlepay(oi.getGoodsid());
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ActiveModule_每日累冲 am_每日累冲 = ActiveModule_每日累冲.get(pcode, false);
            am_每日累冲.addpayvipexp(oi.getAddvipexp(), oi.getGoodsprice());
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ActiveModule_征战日志 am_征战日志 = ActiveModule_征战日志.get(pcode, false);
            am_征战日志.addPayamount(oi.getGoodsprice());
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ActiveModule_累充天数 am_累充天数 = ActiveModule_累充天数.get(pcode, false);
            am_累充天数.addPayDay(oi.getGoodsprice());
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            ActiveModule_周卡 am = ActiveModule_周卡.get(pcode, false);
            if (am.running()) {
                am.unlockorder(oi.getGoodsid(), oi.getMyorderid());
                am.handlebuy(oi.getGoodsid(), oi.getMyorderid(), oi.getAddvipexp());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //许愿活动如果开启，累充也要算上去
        try {
            ActiveModule_心愿瓶 am = ActiveModule_心愿瓶.get(pcode, false);
            if (am.running()) {
                am.addChargeMoney(oi.getGoodsprice());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //寻宝活动如果开启，累充也要算上去
        try {
            ActiveModule_国庆寻宝 am = ActiveModule_国庆寻宝.get(pcode, false);
            if (am.running()) {
                am.addChargeMoney(oi.getGoodsprice());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    static public int getFirstGold(String pcode, int goodsid) {
        try {
            ActiveModule_元宝首冲 am_元宝首冲 = ActiveModule_元宝首冲.get(pcode);
            HashMap<Integer, DateTime> fpmap = am_元宝首冲.getFirstpaymap();
            if (!fpmap.containsKey(goodsid)) {
                return 0;
            }
            DateTime time = fpmap.get(goodsid);
            if (time.isEqual(new DateTime(0))) {
                charge c = DataCenter.getData(goodsid, charge.class);
                return c.getFrgive() - c.getGive();
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    // redis ===================================
}
